<?php
session_start();
require '../model/database.php';
require '../model/functions.php';
include("../model/xl_nguoi_dung.php");

$xl_nguoi_dung = new xl_nguoi_dung();

if (!empty($_GET['openid_ext1_value_firstname']) && !empty($_GET['openid_ext1_value_lastname']) && !empty($_GET['openid_ext1_value_email'])) {    
    $username = $_GET['openid_ext1_value_firstname'] ." ". $_GET['openid_ext1_value_lastname'];
    $email = $_GET['openid_ext1_value_email'];
	$usr = $xl_nguoi_dung->layNguoiDungTheoEmail($email);
	$row1 = $usr[0];
	if(count($usr)>0 && $row1['oauth_provider']!='google')
	{
		header("Location: /doanhiendai/index.php?action=login-facebook-google-fail");
	}
	else{
    	$user = new User();
		$userdata = $user->checkUserGoogle($uid, 'google', $username, $email);
		$row = $userdata[0];
			if(!empty($userdata)){
				$_SESSION['usid'] = $row['MaNguoiDung'];
				$_SESSION['oauth_id'] = $uid;
				$_SESSION['email'] = $row['Email'];
				$_SESSION['fullname'] = $row['TenHienThi'];
				$_SESSION['ava'] = $row['HinhAnhDaiDien'];
				$_SESSION['oauth'] = $row['oauth_provider'];
				header("Location: /doanhiendai/index.php");
		} else {
			header('Location: error.php');
		}
	}
}
?>
